package com.yun.zf.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.app.domain.ZfCity;
import com.yun.zf.common.app.dto.home.OpenCityDto;
import com.yun.zf.common.utils.ChineseCharacterUtil;
import com.yun.zf.common.utils.TreeBuilder;
import com.yun.zf.common.utils.TreeNodes;
import com.yun.zf.system.mapper.ZfCityMapper;
import com.yun.zf.system.service.ZfCityService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 城市区域表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-01-29
 */
@Service
public class ZfCityServiceImpl extends ServiceImpl<ZfCityMapper, ZfCity> implements ZfCityService {
    /**
     * 房源搜索条件 城市列表
     * @param cityCode
     * @return
     */
    @Override
    public  List<TreeNodes>getDistrict(Long cityCode) {
        List<TreeNodes> cityDtos =   baseMapper.getSearchCity(cityCode);
        List<TreeNodes> trees  =  TreeBuilder.buildByRecursive(cityDtos,String.valueOf(cityCode));
        return  trees;
    }

    @Override
    public ZfCity getCity(Long cityCode) {
       QueryWrapper<ZfCity> cityQueryWrapper = new QueryWrapper<>();
       cityQueryWrapper.lambda().eq(ZfCity::getCityCode,cityCode);
       return  baseMapper.selectOne(cityQueryWrapper);
    }

    @Override
    public List<ZfCity> getIsOpenCity() {
        QueryWrapper<ZfCity> cityQueryWrapper =new QueryWrapper<>();
        cityQueryWrapper.lambda().eq(ZfCity::getIsOpen,0).eq(ZfCity::getLevel,"C");

        return baseMapper.selectList(cityQueryWrapper);
    }

    @Override
    public List<Long> getCode(String level) {
        QueryWrapper<ZfCity> cityQueryWrapper = new QueryWrapper<>();
        cityQueryWrapper.lambda().eq(ZfCity::getLevel,level);
        List<ZfCity> cities =baseMapper.selectList(cityQueryWrapper);

        List<Long> codes = cities.stream().map(p -> p.getCityCode()).collect(Collectors.toList());
        return  codes;
    }

    /**
     *  通过城市名称汉字获取拼音简称并更新数据
     * @param level
     * @return
     */
    @Override
    public List<ZfCity> getCity(String level)  {
        QueryWrapper<ZfCity> cityQueryWrapper = new QueryWrapper<>();
        cityQueryWrapper.lambda().isNull(ZfCity::getFirstPinyin).eq(ZfCity::getLevel,level);
        List<ZfCity> cities =baseMapper.selectList(cityQueryWrapper);
        for (ZfCity city:cities
             ) {
            String st =ChineseCharacterUtil.convertHanzi2Pinyin(city.getCityName() ,false) ;
            String py = st.substring(0,1);
            baseMapper.updateAbbreviation(st,city.getId(),py);
            try {
                Thread.sleep(10L);
            }catch (Exception e){

            }

        }
 return  null;
    }

    @Override
    public Map<String, List<OpenCityDto>> getOpenCity() {
         List<OpenCityDto> cityDtos =   baseMapper.getOpenCity();
        return cityDtos.stream().collect(Collectors.groupingBy(OpenCityDto::getFirstPinyin));
    }

    @Override
    public List<OpenCityDto> getHotCity() {
       return  baseMapper.getHotCity();
    }

    @Override
    public List<Tree<String>>  managerOpenCity() {
        List<OpenCityDto> cityDtos =   baseMapper.getManagerCity();
        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        nodeList = cityDtos.stream().map(cityMo -> {
            Map<String, Object> map = new HashMap<>();
            TreeNode<String> treeNode = new TreeNode<String>().setId(cityMo.getCityCode().toString())
                    .setName(cityMo.getCityName())
                    .setParentId(cityMo.getPid().toString())
                    .setExtra(map);
            return treeNode;
        }).collect(Collectors.toList());

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(2);

        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setName(treeNode.getName());
                    //tree.setWeight(treeNode.getWeight());
                });

        return treeList;
    }
}
